package com.qiantu.phone.ui.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.module.BaseDraggableModule;
import com.chad.library.adapter.base.module.DraggableModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.qiantu.api.db.HandleListener;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.RefreshSceneEvent;
import com.qiantu.phone.event.UpdateFavouriteEvent;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SceneOrderActivity extends AppActivity {
    private int mPosition;
    private RecyclerView sceneList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_order;
    }

    @Override
    protected void initView() {
//        getTitleBar().getRightView().setEnabled(false);
        sceneList = findViewById(R.id.scene_list);
        sceneList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new SceneOrderAdapter(LLSceneDBManager.getInstance(getContext()).getAll());
        mAdapter.addChildClickViewIds(R.id.delete);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                int id = view.getId();
                if (id == R.id.delete) {
                    showConfirmCancelHintDialog(getString(R.string.delete_scene_hint), "取消", "删除", new ConfirmCancelHintDialog.OnListener() {
                        @Override
                        public void onLeftClick(BaseDialog dialog) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onRightClick(BaseDialog dialog) {
                            dialog.dismiss();
                            List<String> sceneSerialNoList = new ArrayList<>();
                            SceneBean scene = mAdapter.getData().get(position);
                            sceneSerialNoList.add(scene.getSceneSerialNo());
                            LLHttpManager.deleteScene((LifecycleOwner) getActivity(), sceneSerialNoList, new HttpCallback<HttpData<Boolean>>((OnHttpListener) getActivity()) {
                                @Override
                                public void onSucceed(HttpData<Boolean> result) {
                                    super.onSucceed(result);
                                    LLSceneDBManager.getInstance(getActivity()).remove(mAdapter.getData().get(position));
                                    mAdapter.removeAt(position);
                                    EventBus.getDefault().post(new RefreshSceneEvent(scene.getRoomSerialNo(), scene.getIsFavourite()));
                                }
                            });
                        }
                    });
                }
            }
        });
        mAdapter.getDraggableModule().setOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int i) {
                mPosition = i;
                viewHolder.itemView.setBackgroundResource(R.drawable.setting_item_selector_pressed);
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder viewHolder, int i, RecyclerView.ViewHolder viewHolder1, int i1) {
                /*if (i>i1){
                    sceneList.smoothScrollToPosition(0);
                    Log.i("ssss_up", i+"/"+i1);
                }else if (i<i1){
                    sceneList.smoothScrollToPosition(mAdapter.getItemCount()-1);
                    Log.i("ssss_down", i+"/"+i1);
                }*/
            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int i) {
//                if (mPosition != i) {
//                    getTitleBar().getRightView().setEnabled(true);
//                }
                if (mPosition != i) {
                    doSort();
                }
                viewHolder.itemView.setBackgroundResource(R.drawable.setting_item_pressed_bg);
            }
        });
        mAdapter.getDraggableModule().setDragEnabled(true);
        mAdapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        sceneList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
    }

    private SceneOrderAdapter mAdapter;

    private class SceneOrderAdapter extends BaseQuickAdapter<SceneBean, BaseViewHolder> implements DraggableModule {
        public SceneOrderAdapter(List<SceneBean> data) {
            super(R.layout.scene_order_ly, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SceneBean item) {
            helper.setText(R.id.name, item.getName());
            /*int position = helper.getLayoutPosition();
            RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
            if (position == 0){
                rl.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
                rl.rightMargin = rl.leftMargin;
                rl.topMargin = rl.leftMargin;
                rl.bottomMargin = 0;
            } else if(position == getData().size() - 1){
                rl.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
                rl.rightMargin = rl.leftMargin;
                rl.topMargin = 0;
                rl.bottomMargin = rl.leftMargin;
            }else{
                rl.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
                rl.rightMargin = rl.leftMargin;
                rl.topMargin = 0;
                rl.bottomMargin = 0;
            }*/
        }

        @NotNull
        @Override
        public BaseDraggableModule addDraggableModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
            return new BaseDraggableModule(baseQuickAdapter);
        }
    }

    @Override
    public void onRightClick(View view) {
        doSort();
    }

    private void doSort() {
        LLHttpManager.sceneSort(this, mAdapter.getData(), new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                LLSceneDBManager.getInstance(getActivity()).clearFromDB();
                LLSceneDBManager.getInstance(getActivity()).putInTx(mAdapter.getData(), new HandleListener() {
                    @Override
                    public void onCompleted() {
                        EventBus.getDefault().post(new RefreshSceneEvent());
                        EventBus.getDefault().post(new UpdateFavouriteEvent(UpdateFavouriteEvent.TYPE_ADD));
//                        finish();
                    }
                });
            }
        });
    }
}